

#include "DebugIntegrator.h"

using namespace OBR;

std::map<std::string, DebugMode> DebugIntegrator::nameToDebugMode = {
    {"uv", DebugMode::UV},
    {"albedo", DebugMode::ALBEDO},
    {"normal", DebugMode::NORMAL},
    {"depth", DebugMode::DEPTH},
    {"interaction type", DebugMode::INTERACTION_TYPE},
    {"light id", DebugMode::LIGHT_ID},
    {"light instance id", DebugMode::LIGHT_INSTANCE_ID},
    {"material id", DebugMode::MAT_ID},
    {"material instance id", DebugMode::MAT_INSTANCE_ID},
    {"tr", DebugMode::TR},
    {"custom", DebugMode::CUSTOM},
    {"mesh id", DebugMode::MESH_ID},
    {"mesh instance id", DebugMode::MESH_INSTANCE_ID}};

DebugIntegrator::DebugIntegrator() : Integrator("shaders/Integrator/DebugIntegrator.cu")
{
    this->num_fbos = 1;
    this->program_desc.callables.entryFunctionNameCC = "__continuation_callable__debug_integrator";
    this->program_desc.kind = OPTIX_PROGRAM_GROUP_KIND_CALLABLES;
    this->program_desc.flags = OPTIX_PROGRAM_GROUP_FLAGS_NONE;
}

DebugIntegrator::~DebugIntegrator()
{
}

void DebugIntegrator::_buildFromConfig(const Json::Value &config)
{
    this->settings.mode = nameToDebugMode[config["mode"].asString()];
    if (config.isMember("scale"))
    {
        this->settings.scale = config["scale"].asFloat();
    }
    if (config.isMember("default"))
    {
        ConfigHelpers::loadSpectrum(config["default"], &this->settings.default_val);
    }
}

OptixProgramGroupOptions &DebugIntegrator::getProgramGroupOptions()
{
    return this->program_opt;
}

OptixProgramGroupDesc &DebugIntegrator::getProgramGroupDesc()
{
    this->program_desc.callables.moduleCC = this->cuda_module->optix_module;
    return this->program_desc;
}

void DebugIntegrator::uploadSbtData()
{
    this->sbt_data.resize(sizeof(DebugIntegratorData));
    this->sbt_data.upload(&(this->settings), sizeof(DebugIntegratorData));
}